namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class PaintLineGoldenRatioLine : PaintLineBase
    {
        public PaintLineGoldenRatioLine()
        {
            base.LineType = PaintLineType.GoldenRatioLine;
            base.PlName_zh = "黄金分割线";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                PaintLineBase.PointValue value2 = base.PointList[0];
                float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                PaintLineBase.PointValue value3 = base.PointList[1];
                float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                PaintLineBase.PointValue value4 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = base.PointList[1];
                base.CgInstance.GetRecordByTime(value5.Key);
                int y = base.CgInstance.DicChartDiv[base.OwnedPanelID].Rect.Y;
                float titleHeight = base.CgInstance.DicChartDiv[base.OwnedPanelID].TitleHeight;
                int bottom = base.CgInstance.DicChartDiv[base.OwnedPanelID].Rect.Bottom;
                int height = base.CgInstance.DicChartDiv[base.OwnedPanelID].CalenderScale.Height;
                float x = base.CgInstance.GetX(pos);
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
                base.MoveType = -1;
                if ((crossHairPoint.X >= (x - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (x + (base.LineWidth * 2.5f))))
                {
                    if ((crossHairPoint.Y >= (num2 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num2 + (base.LineWidth * 2.5f))))
                    {
                        base.CgInstance.Cursor = Cursors.SizeWE;
                        base.MoveType = 1;
                        return;
                    }
                    if ((crossHairPoint.Y >= (num - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num + (base.LineWidth * 2.5f))))
                    {
                        base.CgInstance.Cursor = Cursors.SizeWE;
                        base.MoveType = 0;
                        return;
                    }
                }
                base.CgInstance.Cursor = Cursors.Hand;
                base.MoveType = 2;
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                PaintLineBase.PointValue value2 = pList[0];
                float num = (base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value3 = pList[1];
                float num2 = (base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                base.CgInstance.GetRecordByTime(value5.Key);
                float num4 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                StringFormat format = new StringFormat();
                Font font = new Font("New Times Roman", 10f);
                double[] numArray = this.LineParam(pList);
                string[] strArray = new string[] { "0.00%", "23.60%", "38.20%", "50.00%", "61.80%", "100.00%" };
                for (int i = 0; i < numArray.Length; i++)
                {
                    SizeF ef = g.MeasureString(strArray[i], font, 0x3e8, format);
                    float num6 = (((float) numArray[i]) - div.Rect.Y) - div.TitleHeight;
                    g.DrawLine(pen, 0f, num6, base.CgInstance.GetWorkSpaceX(), num6);
                    g.DrawString(strArray[i], font, brush, (float) (base.CgInstance.GetWorkSpaceX() - ef.Width), (float) (num6 - ef.Height));
                }
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(num4 - (base.LineWidth * 2.5f), num - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num4 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                brush.Dispose();
                pen.Dispose();
                format.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                if ((mouseOverIndex >= 0) && (mouseOverIndex <= (base.CgInstance.LastVisibleRecord - 1)))
                {
                    ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                    DataRow row = base.CgInstance.DataSource.Rows[mouseOverIndex];
                    string key = row[base.CgInstance.CalenderScaleField].ToString();
                    base.PointList.Clear();
                    double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                    double num4 = base.CgInstance.GetValueByPoint(mp);
                    base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                    base.PointList.Add(new PaintLineBase.PointValue(1, key, num4));
                    return true;
                }
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                double[] numArray = this.LineParam();
                float num = base.CgInstance.DicChartDiv[base.OwnedPanelID].Rect.Y + base.CgInstance.DicChartDiv[base.OwnedPanelID].TitleHeight;
                float num2 = base.CgInstance.DicChartDiv[base.OwnedPanelID].Rect.Bottom - base.CgInstance.DicChartDiv[base.OwnedPanelID].CalenderScale.Height;
                if ((crossHairPoint.Y >= num) && (crossHairPoint.Y <= num2))
                {
                    foreach (double num3 in numArray)
                    {
                        if ((crossHairPoint.Y >= (num3 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num3 + (base.LineWidth * 2.5f))))
                        {
                            if (base.CgInstance.DataSource.Rows.Count > 0)
                            {
                                int num5 = base.CgInstance.LastVisibleRecord - base.CgInstance.FirstVisibleRecord;
                                int num6 = (base.CgInstance.FirstVisibleRecord - 1) + (num5 / 2);
                                ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                                DataRow row = base.CgInstance.DataSource.Rows[num6];
                                string key = row[base.CgInstance.CalenderScaleField].ToString();
                                PaintLineBase.PointValue value2 = base.PointList[0];
                                double num7 = value2.Value;
                                PaintLineBase.PointValue value3 = base.PointList[1];
                                double num8 = value3.Value;
                                base.PointList.Clear();
                                base.PointList.Add(new PaintLineBase.PointValue(0, key, num7));
                                base.PointList.Add(new PaintLineBase.PointValue(1, key, num8));
                            }
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return this.LineParam(base.PointList);
        }

        public double[] LineParam(List<PaintLineBase.PointValue> pList)
        {
            if (pList.Count == 0)
            {
                return null;
            }
            PaintLineBase.PointValue value2 = pList[0];
            float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
            PaintLineBase.PointValue value3 = pList[1];
            float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
            float num3 = (num <= num2) ? (num + ((num2 - num) * 0.236f)) : (num2 + ((num - num2) * 0.764f));
            float num4 = (num <= num2) ? (num + ((num2 - num) * 0.382f)) : (num2 + ((num - num2) * 0.618f));
            float num5 = (num <= num2) ? (num + ((num2 - num) * 0.5f)) : (num2 + ((num - num2) * 0.5f));
            float num6 = (num <= num2) ? (num + ((num2 - num) * 0.618f)) : (num2 + ((num - num2) * 0.382f));
            return new double[] { ((double) num), ((double) num3), ((double) num4), ((double) num5), ((double) num6), ((double) num2) };
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    switch (base.MoveType)
                    {
                        case 0:
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;

                        case 1:
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;

                        case 2:
                        {
                            double num3 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num4 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num5 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num6 = (num3 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num5 - num4);
                            PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, value2.Key, value3.Value + num6);
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[1];
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, value4.Key, value5.Value + num6);
                            return;
                        }
                    }
                }
            }
        }
    }
}

